package svc;

import constants.Constants;
import dao.CullDao;
import domain.cull.Cull;
import domain.cull.SeasonalityCull;

public class CullSvc {
	CullDao dao = new CullDao();
	
	public Cull getCullDetails(int fileNo){
		Cull result = new Cull();
		result.setCullRiskLact(dao.getCullRiskLact(fileNo));
		result.setCullRiskMim(dao.getCullRiskMim(fileNo));
		result.setSeasonalityCull(dao.getSeasonality(fileNo));
		result.setPind(getProbIndCullTable(result.getSeasonalityCull(), result.getCullRiskMim()));
		return result;
	}
	
	public double[][] getProbIndCullTable(SeasonalityCull seasonalityCull, double cullRiskMim[]){
		double pind[][] = new double[Constants.MIM+1][Constants.SEA+1];
		
		double cullAbs = seasonalityCull.getAbsAdjustment();
		double cullPct = seasonalityCull.getPctAdjustment();
		double cSeasonality = seasonalityCull.getOverallSeasonality();
		double cullSea[] = seasonalityCull.getMonthlySeasonality();
		double cullSeaAvg = getMonthlySeasonalityAvg(seasonalityCull);
		
		for(int sea=1; sea<=Constants.SEA; sea++){
			for(int mim=1; mim<=Constants.MIM; mim++){
				pind[mim][sea] = (((cSeasonality*cullRiskMim[mim]*cullSea[sea])+(1-cSeasonality)*cullRiskMim[mim]*cullSeaAvg)+cullAbs)*(1+cullPct);
			}
		}
		return pind;
	}
	
	private double getMonthlySeasonalityAvg(SeasonalityCull seasonalityCull){
		double sum = 0;
		for(int sea=1; sea<=Constants.SEA; sea++){
			sum += seasonalityCull.getMonthlySeasonality()[sea];
		}
		return sum/Constants.SEA;
	}
	
	public boolean updateCullDetails(int fileNo, Cull cull){
		boolean res1 = dao.updateCullRiskLact(fileNo, cull.getCullRiskLact());
		boolean res2 = dao.updateCullRiskMim(fileNo, cull.getCullRiskMim());
		boolean res3 = dao.updateSeasonality(fileNo, cull.getSeasonalityCull());
		return (res1 && res2 &&  res3);
	}

}
